﻿using ProjectSemIII.Administrator.Process;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSemIII.Administrator.Support
{
    public partial class Support : System.Web.UI.Page
    {
        private FAQSupportProcess process;
        private Entity.RegexUtilities regex;
        private Entity.Support support;
        protected void Page_Load(object sender, EventArgs e)
        {
            process = new FAQSupportProcess();
            regex = new Entity.RegexUtilities();
            if (!Page.IsPostBack)
            {
                this.getSupportInfo();
            }
        }

        protected void btEdit_Click(object sender, EventArgs e)
        {
            if (checkInfomation())
            {
                support = process.getSupportInfo();
                Entity.Support sup = new Entity.Support();
                sup.SupportID = support.SupportID;
                sup.SupportAddress = txtSupportAddress.Text;
                sup.SupportEmail = txtSupportEmail.Text;
                sup.SupportName = txtSupportName.Text;
                sup.SupportPhone = txtSupportPhone.Text;
                int result = process.updateSupportInfo(sup);
                if (result == 10)
                {
                    lbError.Text = "Update support infomation succesfull!";
                    this.getSupportInfo();
                }
                else
                {
                    lbError.ForeColor = System.Drawing.Color.Red;
                    lbError.Text = "Error in update support infomation process!";
                }
            }
        }

        private void getSupportInfo()
        {
            support = process.getSupportInfo();
            txtSupportEmail.Text = support.SupportEmail;
            txtSupportAddress.Text = support.SupportAddress;
            txtSupportName.Text = support.SupportName;
            txtSupportPhone.Text = support.SupportPhone;
        }

        private bool checkInfomation()
        {
            lbError.Text = "";
            bool flag = true;
            double num;
            bool isNumeric = double.TryParse(txtSupportPhone.Text, out num);
            if (txtSupportPhone.Text == "Supporter Phone" || txtSupportPhone.Text == "" || txtSupportPhone.Text == null || !isNumeric)
            {
                lbError.ForeColor = System.Drawing.Color.Red;
                lbError.Text = "Please enter the phone's supporter!";
                flag = false;
            }
            else if (txtSupportName.Text == "Supporter Name" || txtSupportName.Text == "" || txtSupportName.Text == null)
            {
                lbError.ForeColor = System.Drawing.Color.Red;
                lbError.Text = "Please enter the name's supporter!";
                flag = false;
            }
            else if (txtSupportAddress.Text == "Supporter Address" || txtSupportAddress.Text == "" || txtSupportAddress.Text == null)
            {
                lbError.ForeColor = System.Drawing.Color.Red;
                lbError.Text = "Please enter the address's supporter!";
                flag = false;
            }
            else if (txtSupportEmail.Text == "Supporter Email" || txtSupportEmail.Text == "" || txtSupportEmail.Text == null)
            {
                lbError.ForeColor = System.Drawing.Color.Red;
                lbError.Text = "Please enter the email's supporter!";
                flag = false;
            }
            else if (txtSupportPhone.Text.Length <= 6 || txtSupportPhone.Text.Length >= 12)
            {
                lbError.ForeColor = System.Drawing.Color.Red;
                lbError.Text = "Phone's supporter lenght is not format(7-11 numbers)!";
                flag = false;
            }
            else if (!regex.IsValidEmail(txtSupportEmail.Text))
            {
                lbError.ForeColor = System.Drawing.Color.Red;
                lbError.Text = "Email's supporter is not format!";
                flag = false;
            }
            return flag;
        }
    }
}